require 'date2'

class InController < ApplicationController

  before_filter :verify_session
  def step1
  end
  
  def step2
    book = Book.find(:first,:conditions => ["lt_code = ?",params[:book][:lt_code]])
    if book != nil
      @out_record = OutRecord.find(:first,:conditions => ["book_id =?",book.id])
    end
    
    if @out_record == nil
      flash[:error] = "没有这本书的借书记录!"
      else
      @reader = @out_record.reader
      @book = @out_record.book
    end
  end
  
  
  def returnback
    out_record = OutRecord.find(params[:id])
    book = out_record.book;
    book.book_status_id = 1
    histry_rcd = HistryRecord.new
    histry_rcd.reader_name = out_record.reader.reader_name
    histry_rcd.book_name = out_record.book.book_name
    histry_rcd.out_date = out_record.date
    histry_rcd.in_date = Date.today()
    histry_rcd.is_overtime = histry_rcd.in_date <=> out_record.return_date
    begin
      OutRecord.transaction do
#        mailadd = out_record.reader.email
        out_record.destroy
        book.update
        histry_rcd.save!
        email = BookMailer.create_returnbook(histry_rcd,out_record)
        BookMailer.deliver(email)
        flash[:notice]="还书操作成功!"
        redirect_to :action=>'list' ,:controller => 'out'
      end
    rescue
        flash[:notice]="还书操作失败!"
        redirect_to :action=>'list' ,:controller => 'out'
    end
  end
end
